<?php

namespace App\Http\Controllers\V1\API;

use App\Http\Controllers\V1\API\BaseController;
use App\Models\V1\ApplyTrainings;
use App\Models\V1\NewStudent;
use Illuminate\Http\Request;
use App\Models\V1\NewArrival;
use Illuminate\Http\Response;
use Validator;
use Log;

class ApplyTraitDelayController extends BaseController
{
    /****
     * 延迟军训申请接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function applyTraitDelay(Request $request) {
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
        );
        $error = $this->validateApply($request, $result);
        if (!empty($error) || isset($error)) {
            $result = $error;
            return $this->sendResponse($result, $result['errorCode']);
        }
        if ($request->method() === 'POST') {
            $rspObj = $this->handleApply($request);
            if ($rspObj) {
                $result['errorCode'] = Response::HTTP_OK;
                $result['errorMsg'] = '延迟军训申请成功';
            }else{
                $result['errorCode'] = Response::HTTP_BAD_REQUEST;
                $result['errorMsg'] = '延迟军训申请失败';
            }
        }
        return $this->sendResponse($result, $result['errorCode']);
    }


    /****
     * 查看我申请的军训延迟
     * @param Request $request
     */
    public function myApplyTraitDelay(Request $request) {
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
        );

        $error = $this->validateMyApply($request, $result);
        if (!empty($error) || isset($error)) {
            $result = $error;
            return $this->sendResponse($result, $result['errorCode']);
        }
        //申请人
        $userID = $request->get('userID');
        $apply = $this->getApplyTraitByCode($userID);
        if (empty($apply) || !isset($apply)) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = '您未申请军训延迟';
        }else {
            $formatApply = array();
            $formatApply['userID'] = $apply['exam_code'];
            $formatApply['reason'] = $apply['apply_reason'];
            $formatApply['detailReason'] = $apply['other_reason'];
            $formatApply['picPath'] = isset($apply['attach'])?$apply['attach']:'';

            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = '成功';
            $result['data'] = $formatApply;
        }

        return $this->sendResponse($result, $result['errorCode']);
    }


    /***
     * 验证延迟军训申请
     * @param Request $request
     */
    private function validateApply(Request $request) {
        //校验规则
        $rules = array();
        $rules['userID'] = 'required|exists:basic_new_students,exam_code,deleted_at,NULL|bail';
        $rules['reason'] = 'required|bail';
        $rules['detailReason'] = 'required|bail';

        //错误提示信息
        $messages = array();
        $messages['userID.required'] = '用户代码不能为空';
        $messages['userID.exists'] = '用户不存在';
        $messages['detailReason.required'] = '申请详情不能为空';
        $messages['reason.required'] = '申请原因不能为空';

        $validator = Validator::make($request->all(),$rules,$messages);  //第一个参数是提交的数据，第二个是校对规则，第三个是错误信息
        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息
        if(!empty($errors)) {
            $first_errors = $errors[0];
            $result['errorCode'] = Response::HTTP_BAD_REQUEST;
            $result['errorMsg'] = $first_errors;
            return $result;
        }
    }

    /***
     * 验证我的军训延迟申请
     * @param Request $request
     */
    private function validateMyApply(Request $request) {
        //校验规则
        $rules = array();
        $rules['userID'] = 'required|exists:basic_new_students,exam_code,deleted_at,NULL|bail';

        //错误提示信息
        $messages = array();
        $messages['userID.required'] = '用户代码不能为空';
        $messages['userID.exists'] = '用户不存在';

        $validator = Validator::make($request->all(),$rules,$messages);  //第一个参数是提交的数据，第二个是校对规则，第三个是错误信息
        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息
        if(!empty($errors)) {
            $first_errors = $errors[0];
            $result['errorCode'] = Response::HTTP_BAD_REQUEST;
            $result['errorMsg'] = $first_errors;
            return $result;
        }
    }


    private function handleApply(Request $request) {
        /**
         * 传入的参数
         */
        //申请人
        $userID = $request->get('userID');
        //申请信息
        $reason = $request->get('reason');
        $detail = $request->get('detailReason');
        $pic_path = $request->get('picPath');
        $insert_data = [
            'exam_code' => $userID,
            'apply_reason' => $reason,
            'other_reason' => $detail,
            'attach' => $pic_path,
            'arrival_code' => $this->getArrivalCode($userID),
        ];
        $rspObj = ApplyTrainings::updateOrCreate(['exam_code'=>$userID],$insert_data);
        return $rspObj;
    }


    /***
     * 根据考试号获取用户军训延迟申请详情
     * @param $code
     * @return mixed
     */
    private function getApplyTraitByCode($code)
    {
        $apply = ApplyTrainings::where('exam_code',$code)->first();
        return $apply;
    }

    /***
     * 获取当前的迎新代码
     * @return array
     */
    private function getArrivalCode($code)
    {
        $student = NewStudent::where('exam_code',$code)->first();
        return $student['arrival_code'];
    }
}
